package com.gz.purchase.library;

import android.content.Context;

import com.gz.purchase.library.listener.BaseListener;
import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.CrashHandler;
import com.gz.purchase.library.util.PreferenceModel;
import com.gz.purchase.library.util.SingtonAsyncHttpClient;
import com.gz.purchase.library.util.Tools;
import com.loopj.android.http.RequestParams;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * author wanner1991
 * 商品相关方法
 */
public class IProduct {
    private String TAG = IProduct.class.getSimpleName();
    private static IProduct mInstance;
    private static Lock mLock;
    private Context mContext;

    static {
        mLock = new ReentrantLock();
    }

    private IProduct(Context mContext) {
        this.mContext = mContext;
    }

    public static IProduct getInstance(){
        return mInstance;
    }

    public static void init(Context mContext){
        if (mInstance == null){
            mLock.lock();
            try {
                if (mInstance == null) {
                    mInstance = new IProduct(mContext);
                }
            }finally {
                mLock.unlock();
            }
        }
    }

    /**
     * 获取主商品
     * @param page 页码
     * @param product_name  商品名，预留
     * @param Listener 回调接口
     */
    public void getHomeProducts(int page , String product_name,BaseListener Listener){
        if (!Tools.hasInternetConnection(mContext)){
            Listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            params.put("page", page);
            if (product_name != null && !"".equals(product_name)){
                params.put("product_name",product_name);
            }
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_PARCHASE_GET_MAIN_PRODUCTS, params, Listener);
        }

    }

    /**
     * 库存查看界面 查看库存商品
     * @param shop_id  商家id
     * @param product_name 商品名称,选填
     * @param order_type 排序类型,选填
     * @param page
     * @param listener
     */
    public void getStorageCheckProducts(int shop_id,String product_name,int order_type,int page,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            if (shop_id>0){
                params.put("shop_id",shop_id);

            }
            params.put("page", page);
            if (product_name != null && !"".equals(product_name)){
                params.put("product_name",product_name);
            }

            if (order_type > 0) {
                params.put("order_type",order_type);
            }
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_STORAGE_PRODUCTS, params, listener);
        }

    }

    /**
     * 商家列表
     */
    public void getShopList(BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return ;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_GET_OSHOP_LIST,params,listener);

    }

    /**
     * 放入出库车
     * @param product_ids 订单编号，一逗号隔开
     * @param listener
     */
    public void putProductIntoOutStorageCart(String product_ids,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        params.put("product_ids",product_ids);
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_PUT_PRODUCTS_INTO_STORAGE_CART, params, listener);

    }

    /**
     * 获取出库车里商品
     * @param listener
     */
    public void getStorageCartProducts(BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_GET_STORAGE_CART_PRODUCTS, params, listener);
    }

    /**
     * 获取库房模块
     * @param listener
     */
    public void getStorageRoomList(BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_GET_STORAGE_ROOM_LIST, params, listener);
    }

    /**
     * 确认出库
     * @param storage_id 所出往的仓库id,必填
     * @param products_contents 所出商品内容，格式：json "product_id"=>"123","product_num"=>"12"
     * @param listener
     */
    public void  outStorageConfirm(int storage_id,String products_contents,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        params.put("storage_id",storage_id);
        params.put("products_contents",products_contents);
        CrashHandler.i(TAG, params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_OUT_STORAGE_CONFIRM, params, listener);

    }

    /**
     * 入库界面 根据条件查询商品
     * @param search_key
     * @param shop_id
     * @param listener
     */
    public void getSearchProducts(String search_key,int shop_id,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        if (search_key != null && !"".equals(search_key)){
            params.put("search_key",search_key);
        }

        if (shop_id > 0 ){
            params.put("shop_id",shop_id);
        }
        CrashHandler.i(TAG,params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_IN_STORAGE_SEARCH, params, listener);

    }

    /**
     * 获取商家列表
     *
     * @param listener
     */
    public void getShops(BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        CrashHandler.i(TAG,params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_IN_STORAGE_GET_SHOP_LIST, params, listener);

    }

    /**
     * 入库界面 确认入库
     * @param storage_in_type 入库类型,必填
     * @param products_contents 所入商品内容，格式：json "product_id"=>"123","product_num"=>"12",必填
     * @param listener
     */
    public void inStorage(String storage_in_type,String notes,String products_contents,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        params.put("storage_in_type",storage_in_type);
        params.put("storage_note",notes);
        params.put("products_contents",products_contents);
        CrashHandler.i(TAG, params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_IN_STORAGE, params, listener);

    }

    /**
     * （移库出库确确认）入库确认界面获取数据
     * @param listener
     */
    public void getInstorageConfirmInfo(BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_IN_STORAGE_CONFIRM, params, listener);
    }

    /**
     * 移库出库确认界面 同意或者拒绝某条记录
     * @param sign 确认还是拒绝,必填(1确定，0拒绝)
     * @param id  选中的记录id值
     * @param listener
     */
    public void inStorageRejectOrAgree(int sign,int id,BaseListener listener ){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        params.put("sign",sign);
        params.put("id",id);
        CrashHandler.i(TAG, params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_IN_STORAGE_YES_NO, params, listener);
        CrashHandler.i(TAG,Constants.URL_IN_STORAGE_YES_NO+"?"+params+"=======================================");

    }

    /**
     * 入库历史查询
     * @param begin_time 入库记录开始时间 选填
     * @param end_time 入库记录结束时间 选填
     * @param order_type 排序类型 选填
     * @param status 入库记录状态 选填
     * @param listener
     */
    public void inStorageHistoryQuery(String begin_time,String end_time,String status,int order_type,int page,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        if (begin_time != null){
            params.put("begin_time",begin_time);

        }
        if (end_time != null){
            params.put("end_time",end_time);
        }
        if (status != null){
            params.put("status",status);

        }
        if (order_type > 0){
            params.put("order_type",order_type);
        }
        params.put("page",page);
        CrashHandler.i(TAG, params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_IN_STORAGE_HIOSTORY, params, listener);

    }

    /**
     * 出库历史查询
     * @param begin_time 入库记录开始时间 选填
     * @param end_time 入库记录结束时间 选填
     * @param status   记录状态  选填
     * @param order_type 排序类型 选填
     * @param page 页码
     * @param listener
     */
    public void outStorageHistoryQuery(String begin_time,String end_time,String status,int order_type,int page,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        if (begin_time != null){
            params.put("begin_time",begin_time);

        }
        if (end_time != null){
            params.put("end_time",end_time);
        }
        if (status != null){
            params.put("status",status);

        }
        if (order_type > 0){
            params.put("order_type",order_type);
        }
        params.put("page",page);
        CrashHandler.i(TAG, params.toString());
        SingtonAsyncHttpClient.getInstance().post(Constants.URL_OUT_STORAGE_HISTORY, params, listener);

    }

}
